begintownscript;

variables;
short choice;
short afterfight = 0;

body;

beginstate 0; //INIT_STATE

	//groups
	add_range_to_group(7,12,1); //guards
	set_mobility(1001,0); //no moving
	set_attitude(1001,4); //not attacking yet
	set_level(1001,14); //yup, even tougher


	//intro dialog and placement
	if(get_flag(7,0) == 0) {
		message_dialog("Groggily, you return to consciousness...","");
	
		//moving people around
		relocate_character(0,15,16);
		set_character_facing(0,4);
		relocate_character(1,16,15);
		set_character_facing(1,6);
		relocate_character(2,19,16);
		set_character_facing(2,4);
		relocate_character(3,20,15);
		set_character_facing(3,0);
		relocate_character(4,20,16);
		set_character_facing(4,6);
		force_view_center(17,16);
		force_instant_terrain_redraw();
		
		pause(15);
		
		reset_dialog();
		add_dialog_str(0,"Sea spray shocks you into alertness. You're on a boat. A few Loyalists are stationed around the deck, alternately watching the sea and you.",0);
		add_dialog_str(1,"You quickly find that you can't move. This is probably due to the fact that you've been lashed to the boat's masts. A few experimental movements suggest that you can slightly loosen the bonds, so you set to work.",0);
		add_dialog_str(2,"However, before you can make much progress, you hear a door open and close.",0);
		run_dialog(1);
		
		set_flag(7,0,1);
		//So the dialogue script is loaded properly, the cutscene has to begin
		// in the start_state.
		}

break;

beginstate 1; //EXIT_STATE
break;

beginstate 2; //START_STATE
	//Checking for the lead pc each turn... not necessary here
	//pc_counter = 0;
	//while(char_ok(pc_counter) == 0) {
	//	pc_counter = pc_counter + 1;
	//	}
	//lead_char = pc_counter;
	
	//trigger cutscene
	if(get_flag(7,0) == 1) {
		set_flag(7,0,2);
		//main cutscene in state 10
		set_state_continue(10);
		}
	
	//guards jump overboard after Trevahn dies
	if(get_flag(7,3) == 1) {
		if(num_chars_in_group(1) >= 1) {
			reset_dialog();
			add_dialog_str(0,"As the other Loyalists begin to realize that their leader has just been cut down, they all seem to reach the same conclusion.",0);
			add_dialog_str(1,"Before you can even blink, they have all jumped overboard.",0);
			run_dialog(1);
			play_sound(-48); //berserk
			erase_char(1001);
			force_instant_terrain_redraw();
			}
		set_flag(7,3,2);
		}
	
	//post-fight timer
	if((afterfight == 5) && (get_flag(7,4) == 0)) {
		set_flag(7,4,1);
		reset_dialog();
		add_dialog_str(0,"Now that the dust has settled, you find yourself alone on the deck of the ship. You'll have to get back to Stonesthrow eventually, but you don't know how to pilot this ship.",0);
		add_dialog_str(1,"There's always swimming.",0);
		run_dialog(1);
		}
	if((get_flag(7,3) == 2) && (afterfight < 5) && (get_flag(7,4) == 0)) {
		afterfight = afterfight + 1;
		}
	
break;

beginstate 10; //omg teh cutscene
	play_sound(58);  //door open
	play_sound(-59); //door close
	set_flag(7,1,1); //enable Trevahn's dialogue
	activate_hidden_group(1);
	force_instant_terrain_redraw();

break;

beginstate 15; //activate the guards!
	set_attitude(1001,10);
	set_mobility(1001,1);
	reset_dialog();
	add_dialog_str(0,"Trevahn, clearly hurting at this point, growls and signals to the other Loyalists on the deck.",0);
	add_dialog_str(1,"_Get them! Don't just stand there!_",15);
	run_dialog(1);
break;

beginstate 20; //jump over the railing!
	//Only an option if the fight timer is over
	if(get_flag(7,4) == 0)
		end();
	
	reset_dialog();
	add_dialog_str(0,"You approach the edge of the ship, and ponder the swim you have ahead.",0);
	add_dialog_str(1,"Do you dive overboard?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	if(choice == 2) {
		message_dialog("With some difficulty, you manage to swim to shore. Or flail... that might be a more accurate description.","");
		move_to_new_town(8,13,13);
		}
break;